from flask import Flask, render_template, request, redirect, url_for, session, Blueprint
from .utils.auth import query

bp = Blueprint('admin', __name__, url_prefix='/admin')


@bp.route('/', methods=['GET', 'POST'])
def admin():
    if request.method == 'GET':
        sql = "select * from bills"
        result = query(sql)
        return render_template('admin/admin_index.html', result=result)
    else:
        form = request.values.to_dict()
        print('form', form)
        sql = f"UPDATE bills SET amount={form['amount']},reason='{form['reason']}',time=str_to_date('{form['time']}','%Y-%m-%d %T') "\
                + f"WHERE billno={form['billno']}"
        print(sql)
        query(sql)
        return render_template('update_successfully.html')


@bp.route('/admin_update', methods=['GET', 'POST'])
def admin_update():
    if request.method == 'POST':
        billno = request.form.get('billno')
        sql = "select * from bills where billno = '%s'" % billno;

        print(sql)
        result = query(sql)
        print(result)
        if not result:
            return redirect(url_for('update_failed'))
        else:
            user = request.form.get('user')
            amount = request.form.get('amount')
            reason = request.form.get('reason')
            time = request.form.get('time')
            if len(user) != 0:
                sql = "select * from employee where empID = '%s'" % user
                print(sql)
                if not query(sql):
                    return redirect(url_for('update_failed'))
                sql = "update bills set user = '%s' where billno = '%s'" % (user, billno)
                query(sql)
            if len(amount) != 0:
                sql = "update bills set amount = '%s' where billno = '%s'" % (amount, billno)
                query(sql)
            if len(reason) != 0:
                sql = "update bills set reason = '%s' where billno = '%s'" % (reason, billno)
                query(sql)
            if len(time) != 0:
                sql = "update bills set time = '%s' where billno = '%s'" % (time, billno)
                query(sql)
            return redirect(url_for('update_successfully'))
    else:
        return render_template('admin/admin_update.html')


@bp.route('/admin_delete', methods=['GET', 'POST'])
def admin_delete():
    if request.method == 'POST':
        billno = request.form.get('billno')
        user = request.form.get('user')
        amount = request.form.get('amount')
        reason = request.form.get('reason')
        time = request.form.get('time')

        sql = ""
        if len(time) != 0:
            sql = "select * from bills where billno = '%s' or user = '%s' or amount = '%s' or reason = '%s' or time = '%s'" % (
                billno, user, amount, reason, time)
        else:
            sql = "select * from bills where billno = '%s' or user = '%s' or amount = '%s' or reason = '%s'" % (
                billno, user, amount, reason)
        print(sql)
        result = query(sql)
        print(result)
        if not result:
            return redirect(url_for('delete_failed'))
        else:
            sql = "delete " + sql[8:]
            query(sql)
            return redirect(url_for('delete_successfully'))
    else:
        return render_template('admin/admin_delete.html')
